import 'package:convergence_media/di/di.dart';
import 'package:convergence_media/features/api/dtos/navadmin_dtos/admin_admin_login_api_response.dart';
import 'package:convergence_media/features/api/dtos/navadmin_dtos/sms_send_response.dart';
import 'package:convergence_media/features/api/dtos/navadmin_dtos/user_get_token_response.dart';
import 'package:convergence_media/features/api/dtos/navadmin_dtos/user_mobilelogin_response.dart';
import 'package:dio/dio.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'navadmin_api.g.dart';

@riverpod
NavadminApi navadminApi(NavadminApiRef ref) =>
    NavadminApi(dio: ref.watch(navadminDioProvider));

/// {@template navadmin_api}
/// 综合运营平台导航系统后台
/// {@endtemplate}
class NavadminApi {
  /// {@macro navadmin_api}
  NavadminApi({required Dio dio}) : _dio = dio;

  /// nodoc
  final Dio _dio;

  /// /user/mobilelogin
  /// 登录并获取应用key
  FutureOr<UserMobileloginResponse> postUserMobilelogin(
    String phone,
    String captcha,
  ) async {
    final uri = Uri(
      path: '/user/mobilelogin',
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'mobile': phone,
        'captcha': captcha,
        'event': 'login',
      }),
    );

    return UserMobileloginResponse.fromJson(response.data!);
  }

  /// /sms/send
  /// 发送验证码
  FutureOr<SmsSendResponse> postSmsSend(String phone) async {
    final uri = Uri(
      path: '/sms/send',
      queryParameters: {
        'phone': phone,
      },
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'mobile': phone,
        'event': 'login',
      }),
    );

    return SmsSendResponse.fromJson(response.data!);
  }

  /// /admin/adminLoginApi
  FutureOr<AdminAdminLoginApiResponse> postAdminAdminLoginApi(
    String username,
    String password,
    String captcha,
  ) async {
    final uri = Uri(
      path: '/admin/adminLoginApi',
      queryParameters: {
        'username': username,
        'password': password,
        'captcha': captcha,
      },
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'username': username,
        'password': password,
        'captcha': captcha,
      }),
    );

    return AdminAdminLoginApiResponse.fromJson(response.data!);
  }

  /// /user/getToken
  /// 通过token换取用户ID
  FutureOr<UserGetTokenResponse> postUserGetToken(
    String token,
    String threesoftname,
    String key,
  ) async {
    final uri = Uri(
      path: '/user/getToken',
      queryParameters: {},
    );

    final response = await _dio.postUri<Map<String, dynamic>>(
      uri,
      data: FormData.fromMap({
        'token': token,
        'threesoftname': threesoftname,
        'key': key,
      }),
    );

    return UserGetTokenResponse.fromJson(response.data!);
  }
}
